/**
 * 
 */
$(function() {
	
	
	var str = window.location.search;
	var arr = str.split("=");
	alert(window.location);
	alert(arr[1]);
	$.ajax({
		type : 'post',
		url : 'device/getDeviceById.action?id=' + arr[1],
		dateType : 'json',
		success : function(device) {
			
			var depart = $('#department');
			var region = $('#region');
			var building = $('#building');
			var room = $('#room');
			var devicetype = $('#devicetype');
			depart.append("<option value=''>--请选择--</option>");
			region.append("<option value=''>--请选择--</option>");
			building.append("<option value=''>--请选择--</option>");
			room.append("<option value=''>--请选择--</option>");
			devicetype.append("<option value=''>--请选择--</option>");
			$.ajax({
				type : 'post',
				url : 'department/getDepartment.action',
				dataType : 'json',
				success : function(data) {
					for(var i=0; i<data.length; i++) {
						if(data[i].id == device.departmentId) {
							depart.append("<option value=" + data[i].id + " selected =\"true\">" + data[i].name + "</option>");
						} else {
							depart.append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
						}					
					}	
				}
			});
			$.ajax({
				type : 'post',
				url : 'region/getRegion.action',
				dataType : 'json',
				success : function(data) {
					for(var i=0; i<data.length; i++) {
						if(data[i].id == device.regionId) {
							region.append("<option value=" + data[i].id + " selected =\"true\">" + data[i].name + "</option>");
						} else {
							region.append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
						}
					}					
				}
			});		
			
			$.ajax({
				type : 'post',
				url : 'build/getBuilding.action?regionId=' + device.regionId,
				dataType : 'json',
				success : function(data) {
					for(var i=0; i<data.length; i++) {
						if(data[i].id == device.buildingId) {
							building.append("<option value=" + data[i].id + " selected =\"true\">" + data[i].name + "</option>");
						} else {
							building.append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
						}
					}					
				}
			});
			
			$.ajax({
				type : 'post',
				url : 'room/getRoom.action?buildingId=' + device.buildingId,
				dataType : 'json',
				success : function(data) {
					for(var i=0; i<data.length; i++) {
						if(data[i].id == device.roomId) {
							room.append("<option value=" + data[i].id + " selected =\"true\">" + data[i].name + "</option>");
						} else {
							room.append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
						}
					}					
				}
			});
			document.getElementById("name").value = device.name;
			document.getElementById("number").value = device.number;
			document.getElementById("model").value = device.model;
			document.getElementById("power").value = device.power;
			document.getElementById("voltage").value = device.voltage;
			document.getElementById("current").value = device.current;
			document.getElementById("devicedate").value = device.devicedate;
			document.getElementById("environment").value = device.environment;
			var obj = document.getElementById("status");
			for(var i=0; i<obj.length; i++) {
				if(obj[i].value == device.status)
					obj[i].selected = true;
			}
		}
	});
});

function onBuilding() {
	var building = $('#building');
	var room = $('#room');
	var regionId = $('#region').val();
	building.empty();
	room.empty();
	building.append("<option value=''>--请选择--</option>");
	$.ajax({
		type : 'post',
		url : 'build/getBuilding.action?regionId=' + regionId,
		dataType : 'json',
		success : function(data) {
			for(var i=0; i<data.length; i++) {
				building.append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
			}					
		}
	});
}

function onRoom() {
	var room = $('#room');
	var buildingId = $('#building').val();
	room.empty();
	room.append("<option value=''>--请选择--</option>");
	$.ajax({
		type : 'post',
		url : 'room/getRoom.action?buildingId=' + buildingId,
		dataType : 'json',
		success : function(data) {
			for(var i=0; i<data.length; i++) {
				room.append("<option value=" + data[i].id + ">" + data[i].name + "</option>");
			}					
		}
	});
}

function updateDevice() {
	var str = window.location.search;
	var arr = str.split("=");
	$.ajax({
		type : 'post',
		url : 'device/updateDevice.action?id=' + arr[1],
		data : {
			department : $('#department').val(),
			region : $('#region').val(),
			building : $('#building').val(),
			room : $('#room').val(),
			devicetype : $('#devicetype').val(),
			status : $('#status').val(),
			name : $('#name').val(),
			number : $('#number').val(),
			model : $('#model').val(),
			power : $('#power').val(),
			voltage : $('#voltage').val(),
			current : $('#current').val(),
			devicedate : $('#devicedate').val(),
			environment : $('#environment').val()		
		},
		dataType: 'html',
		success : function(data) {
			$("#main-content").html(data);
		}
	});
}